<?php

namespace App\Http\Controllers;

use App\Clubbanner;
use App\Http\Requests\AddClubbannerRequest;
use App\MakeTable\Table;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;
use Gate;
class ClubbannerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
   public function index()
    {
        $pagename = '俱乐部轮播图管理';
        $modelurl = 'clubbanners';
        $tablecate = ['tablecate'=>'俱乐部轮播图'];
        $headname = ['标题','图片','操作'];
        $tablehead = ['title','image_url','id'];
        $clubbannertable = new Table($tablehead,$page = 8);
        if (Gate::allows('club_Admin_club')) {
            $model = $clubbannertable->viewdata(Clubbanner::class);
            $models = $clubbannertable->viewpagedata(Clubbanner::class);
        }
        else{
            $model = $clubbannertable->viewdatawithuser(Clubbanner::class);
            $models = $clubbannertable->viewpagedatawithuser(Clubbanner::class);
        }
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //添加轮播图
        $modelurl = 'clubbanners';
        $pagename = '新增轮播图';
        $formhead = ['title','image_url'];
        $formname = ['标题','图片'];
        $formhead = collect($formname)->zip($formhead)->toArray();//压缩成数组
        $mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');
        return view('admin.home.clubbannerform',compact('pagename','clubbanners','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/activityform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddClubbannerRequest $request)
    {
        //添加验证
        $data = array_merge(
            ['club_id'=>\Auth::user()->clubroleid(),],
            $request->all(),
            SoftImage::url($request->file('image_url'))//处理图片的函数
        );
       //dd($data);
        $clubbannner = Clubbanner::create($data);
//        $soft->tags()->attach($request->input('tags_list')); 多对多
        return redirect('admin/clubbanners')->with('message', '成功添加'.$clubbannner->name);
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $clubbanner = Clubbanner::findOrFail($id);
        $modelurl = 'clubbanners';
        $pagename = $clubbanner->name;
        $formhead = ['title','image_url'];
        $formname = ['标题','图片'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        return view('admin.home.editclubbanner',compact('pagename','clubbanner','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //添加验证
        $clubbanner = Clubbanner::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all(),
            SoftImage::url($request->file('image_url'))//处理图片的函数
        );
        $clubbanner->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Clubbanner::destroy($id);
        return Success::back();
    }
}
